<?php

namespace App\Http\Resources;

use App\Models\Factory;
use App\Models\Setting;
use http\Url;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

class FactoryResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param \Illuminate\Http\Request $request
     * @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
     */
    public function toArray($request)
    {
        $item = $this->resource;
        /**
         * @var $item \App\Models\Factory
         */
        if ($item->type == 1) {
            $unit = '元/㎡';
            $totalUnit='元/月';
            $total_price=$item->total_price;
        } elseif ($item->type == 2) {
            $unit = '元/㎡';
            $totalUnit='万元';
            $total_price=    round($item->total_price/10000,1);;

        } else {
            $unit = $item->price . '元/月';
            if (!$item->total_price||$item->total_price==0){
                $totalUnit='';
                $total_price='面谈';
            }else{
                $totalUnit='万元';
                $total_price=round($item->total_price/10000,1);
            }

        }
        $peitao= [];
        if ($item->peitao->isNotEmpty()){
           $peitao= $item->peitao->pluck('id')->toArray();
        }
        /**
         * @var Factory $item
         */
        return [
            'code' => 200,
            'message' => 'success',
            'data' => [
                'id' => $item->id,
                'title' => $item->title,
                'logo' => \Storage::url($item->logo),
                'tag' => $item->tag->reject(function ($value, $key) {
                    return $key > 2;
                })->pluck('name')->toArray(),
                'area' =>  $item->area,
                'price' =>$item->price,
                'price_unit'=>$unit,
                'total_price'=>$total_price,
                'total_unit'=>$totalUnit,
                'lat'=>$item->baidu_lat,
                'lng'=>$item->baidu_lng,
                'type'=>$item->type,
                'type_desc'=>Factory::$mapType[$item->type] ?? '',
                'images'=>collect($item->images)->map(function ($item){
                    try {
                      $url=  \Storage::url($item);
                    }catch (\Exception $exception){
                        $url='';
                    }
                    return $url;
                }),
                'address'=>($item->city->name ?? '').($item->town->name ?? '')/*.$item->address*/,
                'rent_type'=>$item->rentType->name ?? '',
                'rent_nature'=>$item->rentNature->name ?? '',
                'contact_mobile'=>$item->broker_mobile,
                'contact_name'=>$item->broker_name,
                'broker_mobile'=>$item->broker_mobile,
                'broker_name'=>$item->broker_name,
                'city'=>$item->city->name ?? '',
                'city_id'=>$item->city_id,
                'view_num'=>$item->view_num,
                'floor'=>$item->floor,
                'guige'=>'进深:'.$item->jinshen.'米，开间:'.$item->kaijian.'米，层高:'.$item->cenggao.'米',
                'content'=>$item->content,
                'peitao'=>$peitao,
                'status'=>$item->status,
                'created_at'=>Carbon::parse($item->created_at)->format('Y-m-d')
            ]
        ];
    }
}
